/**
 * Copyright (c) Huawei Technologies Co., Ltd. 1987-2021. All rights reserved.
*/

package com.gitee.fufu669.config;

import com.gitee.fufu669.config.redis.ShardedJedisPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

//懒加载
@Lazy
// 配置注解
@Configuration
/** @author wangfupeng */
@ComponentScan(basePackages = {"com.gitee.fufu669"})
/**
 * TODO
*/
public class ThreadPoolTaskExecutorConfig {

    // 日志对象
    private static final Logger logger = LoggerFactory.getLogger(ThreadPoolTaskExecutorConfig.class);

    // 依赖注入值的注解
    @Value("${threadpooltaskexecutor.corepoolsize:5}")
    // TODO
    private String corepoolsize;
    // 依赖注入值的注解
    @Value("${threadpooltaskexecutor.maxpoolsize:50}")
    // TODO
    private String maxpoolsize;
    // 依赖注入值的注解
    @Value("${threadpooltaskexecutor.queuecapacity:100000}")
    // TODO
    private String queuecapacity;
    // 依赖注入值的注解
    @Value("${threadpooltaskexecutor.keepaliveseconds:60}")
    // TODO
    private String keepaliveseconds;
    // 依赖注入值的注解
    @Value("${threadpooltaskexecutor.threadnameprefix:threadPoolTaskExecutor-}")
    // TODO
    private String threadnameprefix;

    // 把对象构造出来注入容器的注解
    @Bean(name="threadPoolTaskExecutorCacheFetchLater")
    /**
     * TODO
    */
    public Executor threadPoolTaskExecutor() {
        // 创建
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 转换值
        executor.setCorePoolSize(Integer.valueOf(corepoolsize));
        // 转换值
        executor.setMaxPoolSize(Integer.valueOf(maxpoolsize));
        // 转换值
        executor.setQueueCapacity(Integer.valueOf(queuecapacity));
        // 转换值
        executor.setKeepAliveSeconds(Integer.valueOf(keepaliveseconds));
        // TODO
        executor.setThreadNamePrefix(threadnameprefix);
        // TODO
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // TODO
        executor.initialize();
        // 返回
        return executor;
    }

}
